package ua.amsoft.brelok.manager;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

/**
 * App Widget Shared Preferences manager
 * 
 * @author Tair Mustafaiev
 */
public class PreferencesManager {
    
    static final String PREFS_NAME = "ua.amsoft.test.idiagramm.manager.WidgetConfigurationManager";
    
    /**
     * Returns {@link SharedPreferences} for current {@link Context}
     * 
     * @param context
     * @return {@link SharedPreferences} object
     */
    public SharedPreferences getAppPreferences(final Context context) {
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return prefs;
    }
    
    /**
     * Read the prefix from the SharedPreferences object for this widget. If
     * there is no preference saved, get the default from a resource
     * 
     * @param context
     * @param key
     * @param defaultValue
     * @return
     */
    public String getPrefByName(final Context context, final String key, final String defaultValue) {
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        final String value = prefs.getString(key, defaultValue);
        return value;
    }
    
    /**
     * Deletes {@link SharedPreferences} when widget is deleted
     * 
     * @param context
     */
    public void deletePrefs(final Context context) {
        final SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(
                context).edit();
        prefs.clear();
        prefs.commit();
    }
    
}
